/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frontera;

import entidad.Medicion;
import entidad.Persistencia;
import java.util.ArrayList;

/**
 *
 * @author Admin
 */
public class panelReporte extends javax.swing.JPanel {

    /**
     * Creates new form panelReporte
     */
    public panelReporte() {
        initComponents();
        rellenarTabla(null, null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup_Tendencia_Central = new javax.swing.ButtonGroup();
        buttonGroup_Dispersion = new javax.swing.ButtonGroup();
        jPanel_Tendencia_Central = new javax.swing.JPanel();
        jRadioButton_Media = new javax.swing.JRadioButton();
        jRadioButton_Mediana = new javax.swing.JRadioButton();
        jRadioButton_Moda = new javax.swing.JRadioButton();
        jPanel_Dispersion = new javax.swing.JPanel();
        jRadioButton_Varianza = new javax.swing.JRadioButton();
        jRadioButton_Desviacion_Estandar = new javax.swing.JRadioButton();
        jRadioButton_Rango_Intercuartilico = new javax.swing.JRadioButton();
        jScrollPane_Datos_Estadisticos = new javax.swing.JScrollPane();
        jTable_Datos_Estadisticos = new javax.swing.JTable();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Reporte"));

        jPanel_Tendencia_Central.setBorder(javax.swing.BorderFactory.createTitledBorder("Medidas de Tendencia Central"));

        buttonGroup_Tendencia_Central.add(jRadioButton_Media);
        jRadioButton_Media.setText("Media");
        jRadioButton_Media.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton_MediaActionPerformed(evt);
            }
        });

        buttonGroup_Tendencia_Central.add(jRadioButton_Mediana);
        jRadioButton_Mediana.setText("Mediana");
        jRadioButton_Mediana.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton_MedianaActionPerformed(evt);
            }
        });

        buttonGroup_Tendencia_Central.add(jRadioButton_Moda);
        jRadioButton_Moda.setText("Moda");
        jRadioButton_Moda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton_ModaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel_Tendencia_CentralLayout = new javax.swing.GroupLayout(jPanel_Tendencia_Central);
        jPanel_Tendencia_Central.setLayout(jPanel_Tendencia_CentralLayout);
        jPanel_Tendencia_CentralLayout.setHorizontalGroup(
            jPanel_Tendencia_CentralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel_Tendencia_CentralLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel_Tendencia_CentralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRadioButton_Media)
                    .addComponent(jRadioButton_Mediana)
                    .addComponent(jRadioButton_Moda))
                .addContainerGap(97, Short.MAX_VALUE))
        );
        jPanel_Tendencia_CentralLayout.setVerticalGroup(
            jPanel_Tendencia_CentralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel_Tendencia_CentralLayout.createSequentialGroup()
                .addComponent(jRadioButton_Media)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton_Mediana)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton_Moda))
        );

        jPanel_Dispersion.setBorder(javax.swing.BorderFactory.createTitledBorder("Medidas de Dispersión"));

        buttonGroup_Dispersion.add(jRadioButton_Varianza);
        jRadioButton_Varianza.setText("Varianza");
        jRadioButton_Varianza.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton_VarianzaActionPerformed(evt);
            }
        });

        buttonGroup_Dispersion.add(jRadioButton_Desviacion_Estandar);
        jRadioButton_Desviacion_Estandar.setText("Desviación Estándar");
        jRadioButton_Desviacion_Estandar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton_Desviacion_EstandarActionPerformed(evt);
            }
        });

        buttonGroup_Dispersion.add(jRadioButton_Rango_Intercuartilico);
        jRadioButton_Rango_Intercuartilico.setText("Rango Intercuartílico");
        jRadioButton_Rango_Intercuartilico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton_Rango_IntercuartilicoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel_DispersionLayout = new javax.swing.GroupLayout(jPanel_Dispersion);
        jPanel_Dispersion.setLayout(jPanel_DispersionLayout);
        jPanel_DispersionLayout.setHorizontalGroup(
            jPanel_DispersionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel_DispersionLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel_DispersionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRadioButton_Varianza)
                    .addComponent(jRadioButton_Desviacion_Estandar)
                    .addComponent(jRadioButton_Rango_Intercuartilico))
                .addContainerGap(23, Short.MAX_VALUE))
        );
        jPanel_DispersionLayout.setVerticalGroup(
            jPanel_DispersionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel_DispersionLayout.createSequentialGroup()
                .addComponent(jRadioButton_Varianza)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton_Desviacion_Estandar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jRadioButton_Rango_Intercuartilico))
        );

        jTable_Datos_Estadisticos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Variable", "Title 2", "Title 3"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable_Datos_Estadisticos.setEnabled(false);
        jScrollPane_Datos_Estadisticos.setViewportView(jTable_Datos_Estadisticos);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane_Datos_Estadisticos, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel_Tendencia_Central, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel_Dispersion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel_Dispersion, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel_Tendencia_Central, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane_Datos_Estadisticos, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jRadioButton_MediaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton_MediaActionPerformed
        javax.swing.JRadioButton b = (javax.swing.JRadioButton) evt.getSource();
        rellenarTabla(radioButtonDispersion(), b);
        
    }//GEN-LAST:event_jRadioButton_MediaActionPerformed

    private void jRadioButton_MedianaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton_MedianaActionPerformed
        javax.swing.JRadioButton b = (javax.swing.JRadioButton) evt.getSource();
        rellenarTabla(radioButtonDispersion(), b);
    }//GEN-LAST:event_jRadioButton_MedianaActionPerformed

    private void jRadioButton_ModaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton_ModaActionPerformed
        javax.swing.JRadioButton b = (javax.swing.JRadioButton) evt.getSource();
        rellenarTabla(radioButtonDispersion(), b);
    }//GEN-LAST:event_jRadioButton_ModaActionPerformed

    private void jRadioButton_VarianzaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton_VarianzaActionPerformed
        javax.swing.JRadioButton b = (javax.swing.JRadioButton) evt.getSource();
        rellenarTabla(b, radioButtonTendencia());
    }//GEN-LAST:event_jRadioButton_VarianzaActionPerformed

    private void jRadioButton_Desviacion_EstandarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton_Desviacion_EstandarActionPerformed
        javax.swing.JRadioButton b = (javax.swing.JRadioButton) evt.getSource();
        rellenarTabla(b, radioButtonTendencia());
    }//GEN-LAST:event_jRadioButton_Desviacion_EstandarActionPerformed

    private void jRadioButton_Rango_IntercuartilicoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton_Rango_IntercuartilicoActionPerformed
        javax.swing.JRadioButton b = (javax.swing.JRadioButton) evt.getSource();
        rellenarTabla(b, radioButtonTendencia());
    }//GEN-LAST:event_jRadioButton_Rango_IntercuartilicoActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup_Dispersion;
    private javax.swing.ButtonGroup buttonGroup_Tendencia_Central;
    private javax.swing.JPanel jPanel_Dispersion;
    private javax.swing.JPanel jPanel_Tendencia_Central;
    private javax.swing.JRadioButton jRadioButton_Desviacion_Estandar;
    private javax.swing.JRadioButton jRadioButton_Media;
    private javax.swing.JRadioButton jRadioButton_Mediana;
    private javax.swing.JRadioButton jRadioButton_Moda;
    private javax.swing.JRadioButton jRadioButton_Rango_Intercuartilico;
    private javax.swing.JRadioButton jRadioButton_Varianza;
    private javax.swing.JScrollPane jScrollPane_Datos_Estadisticos;
    private javax.swing.JTable jTable_Datos_Estadisticos;
    // End of variables declaration//GEN-END:variables

    /**
     * Este metodo se encarga de buscar que <code>radiobutton</code> del grupo Dispersion
     * esta seleccionado
     * @return El <code>radioButton</code> seleccionado
     */
    private javax.swing.JRadioButton radioButtonDispersion(){
        javax.swing.JRadioButton b = null;
        for(java.awt.Component c: jPanel_Dispersion.getComponents()){
            b = (javax.swing.JRadioButton) c;            
            if(b.isSelected()){
                break;
            }
        }
        
        return b;
    }
    
    /**
     * Este metodo se encarga de buscar que <code>radiobutton</code> del grupo Tendencia Central
     * esta seleccionado
     * @return El <code>radioButton</code> seleccionado
     */
    private javax.swing.JRadioButton radioButtonTendencia(){
        javax.swing.JRadioButton b = null;
        for(java.awt.Component c: jPanel_Tendencia_Central.getComponents()){
            b = (javax.swing.JRadioButton) c;            
            if(b.isSelected()){
                break;
            }
        }
        
        return b;
    }
    
    /**
     * Este metodo se encarga de ingresar los datos dentro de la tabla
     * @param disp <code>RadioButton</code> del grupo Dispersion
     * @param tend <code>RadioButton</code> del grupo Tendencia Central
     */
    private void rellenarTabla(javax.swing.JRadioButton disp, javax.swing.JRadioButton tend){
        String[] col = new String[3];
        if (disp != null && tend != null) {
            col[0] = "Variables";
            col[1] = tend.getText();
            col[2] = disp.getText();
            
            String[][] rows = new String[3][3];
            
            rows[0][0] = "Temperatura";
            rows[1][0] = "Pluviosidad";
            rows[2][0] = "Presión Atmosferica";
            
            ingresarEstadisticas(rows, col);
            
            javax.swing.table.DefaultTableModel model = new javax.swing.table.DefaultTableModel(rows, col);
            jTable_Datos_Estadisticos.setModel(model);
        }
        else{
            col[0] = "Variables";
            col[1] = " ";
            col[2] = " ";
            javax.swing.table.DefaultTableModel model = new javax.swing.table.DefaultTableModel(null, col);
            jTable_Datos_Estadisticos.setModel(model);
        }
    }
    
    /**
     * Inserta dentro de una matriz los datos estadisticos de cada variable
     * @param table Matriz que va a almacenar los datos
     * @param names Arreglo que contiene los nombres de los datos estadisticos a ingresar
     */
    private void ingresarEstadisticas(String[][] table, String[] names){
        Persistencia per = Persistencia.getInstance();
        ArrayList<Double> temp = new ArrayList<Double>();
        ArrayList<Double> pluv = new ArrayList<Double>();
        ArrayList<Double> pres = new ArrayList<Double>();
        
        for(Medicion m: per.getHistorico()){
            temp.add(m.getTemperatura());
            pluv.add(m.getPluviosidad());
            pres.add(m.getPresionAtmosferica());
        }
        
        if (temp.size() > 2 && pluv.size() > 2 && pres.size() > 2) {
            if (names[1].equals("Media")) {
                table[0][1] = Double.toString(new control.ComponenteMatematico().Media(temp));
                table[1][1] = Double.toString(new control.ComponenteMatematico().Media(pluv));
                table[2][1] = Double.toString(new control.ComponenteMatematico().Media(pres));
            } else if (names[1].equals("Mediana")) {
                table[0][1] = Double.toString(new control.ComponenteMatematico().Mediana(temp));
                table[1][1] = Double.toString(new control.ComponenteMatematico().Mediana(pluv));
                table[2][1] = Double.toString(new control.ComponenteMatematico().Mediana(pres));
            } else {
                table[0][1] = Double.toString(new control.ComponenteMatematico().Moda(temp));
                table[1][1] = Double.toString(new control.ComponenteMatematico().Moda(pluv));
                table[2][1] = Double.toString(new control.ComponenteMatematico().Moda(pres));
            }
            
            if (names[2].equals("Varianza")) {
                table[0][2] = Double.toString(new control.ComponenteMatematico().Varianza(temp));
                table[1][2] = Double.toString(new control.ComponenteMatematico().Varianza(pluv));
                table[2][2] = Double.toString(new control.ComponenteMatematico().Varianza(pres));
            } else if (names[2].equals("Desviación Estándar")) {
                table[0][2] = Double.toString(new control.ComponenteMatematico().Desviacion(temp));
                table[1][2] = Double.toString(new control.ComponenteMatematico().Desviacion(pluv));
                table[2][2] = Double.toString(new control.ComponenteMatematico().Desviacion(pres));
            } else {
                table[0][2] = Double.toString(new control.ComponenteMatematico().RangoInterCuartilico(temp));
                table[1][2] = Double.toString(new control.ComponenteMatematico().RangoInterCuartilico(pluv));
                table[2][2] = Double.toString(new control.ComponenteMatematico().RangoInterCuartilico(pres));
            }
        }
    }
}
